alert('SceneMore.js loaded');


var umdex=0,index1=0,index2=0,index3=0;
var datatext;
var datameotieude = new Array();
var datameonoidung = new Array();
var datachedotieude = new Array();
var datachedonoidung = new Array();
var datadichotieude = new Array();
var datadichonoidung = new Array();
////////////////////
function LoadDataMore(){
	  if  (window.XMLHttpRequest)
	  {
		xhttp=new  XMLHttpRequest();
	  }
	  else
	  {
		  xhttp=new  ActiveXObject("Microsoft.XMLHTTP");
	  }
	  xhttp.open("GET","app/data/morong.xml",false);
	
		xhttp.send();
		xmlDoc=xhttp.responseXML;
		x=xmlDoc.getElementsByTagName("meonauan");
		y=xmlDoc.getElementsByTagName("chedoan");
		z=xmlDoc.getElementsByTagName("dicho");
		
		for (var i=0;i<x.length;i++){
			 datameotieude.push(x[i].getElementsByTagName("tieude")[0].childNodes[0].nodeValue);
			 datameonoidung.push(x[i].getElementsByTagName("noidung")[0].childNodes[0].nodeValue);
		}
		//alert("cai i la: " +datameotieude[3]);
		// alert("cai i la: " +datameonoidung[3]);
		for (var i=0;i<y.length;i++){
			 datachedotieude.push(y[i].getElementsByTagName("tieude")[0].childNodes[0].nodeValue);
			 datachedonoidung.push(y[i].getElementsByTagName("noidung")[0].childNodes[0].nodeValue);
			 alert("thong baooooooooooooooooooooooooooooooooooooooooooooo"+  datachedonoidung[i]);
		}
		for (var i=0;i<z.length;i++){
			 datadichotieude.push(z[i].getElementsByTagName("tieude")[0].childNodes[0].nodeValue);
			 datadichonoidung.push(z[i].getElementsByTagName("noidung")[0].childNodes[0].nodeValue);
		}
		
}

//////////////////////



//Ham khoi gan
function Moreini(){
	document.images['mitem1'].src="images/Morong/meonauanfocus.png";
	document.images['mitem2'].src="images/Morong/chedoan.png";
	document.images['mitem3'].src="images/Morong/dicho.png";
	$('#morelist').sfList('show');      
	$('#morelist').sfList('focus');
	$('#morelistchedoan').sfList('hide');
	$('#morelistchedoan').sfList('blur');
	$('#morelistdicho').sfList('hide');
	$('#morelistdicho').sfList('blur');
	document.getElementById("moretieude").innerHTML = datameotieude[$('#morelist').sfList('getIndex')];
	document.getElementById("morenoidung").innerHTML = datameonoidung[$('#morelist').sfList('getIndex')];
}
///////
function MoreLeft(){
	document.images['mitem1'].src="images/Morong/meonauanfocus.png";
	if(umdex==0){alert("umdex hien tai la: "+umdex);
		umdex=2;
		document.images['mitem1'].src="images/Morong/meonauan.png";
		document.images['mitem2'].src="images/Morong/chedoan.png";
		document.images['mitem3'].src="images/Morong/dichofocus.png";
		
		$('#morelist').sfList('hide');     
		$('#morelist').sfList('blur');
		$('#morelistchedoan').sfList('blur');
		$('#morelistchedoan').sfList('hide');
		$('#morelistdicho').sfList('show');
		$('#morelistdicho').sfList('focus');
		
		document.getElementById("moretieude").innerHTML = datadichotieude[$('#morelistdicho').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datadichonoidung[$('#morelistdicho').sfList('getIndex')];


	}else if (umdex==1) {alert("umdex hien tai la: "+umdex);
		umdex--;
		document.images['mitem1'].src="images/Morong/meonauanfocus.png";
		document.images['mitem2'].src="images/Morong/chedoan.png";
		document.images['mitem3'].src="images/Morong/dicho.png";
		$('#morelist').sfList('show');      
		$('#morelist').sfList('focus');
		$('#morelistchedoan').sfList('hide');
		$('#morelistchedoan').sfList('blur');
		$('#morelistdicho').sfList('hide');
		$('#morelistdicho').sfList('blur');
		//set lại noi dung
		document.getElementById("moretieude").innerHTML = datameotieude[$('#morelist').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datameonoidung[$('#morelist').sfList('getIndex')];
	}else if (umdex==2) {alert("umdex hien tai la: "+umdex);
		umdex--;
		document.images['mitem1'].src="images/Morong/meonauan.png";
		document.images['mitem2'].src="images/Morong/chedoanfocus.png";
		document.images['mitem3'].src="images/Morong/dicho.png";
		$('#morelist').sfList('blur');   
		$('#morelist').sfList('hide');
		$('#morelistchedoan').sfList('show');
		$('#morelistchedoan').sfList('focus');
		$('#morelistdicho').sfList('hide');
		$('#morelistdicho').sfList('blur');
		
		document.getElementById("moretieude").innerHTML = datachedotieude[$('#morelistchedoan').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datachedonoidung[$('#morelistchedoan').sfList('getIndex')];
	}}

function MoreRight(){
	
	if(umdex==0){alert("umdex hien tai la: "+umdex);
		umdex++;
		document.images['mitem1'].src="images/Morong/meonauan.png";
		document.images['mitem2'].src="images/Morong/chedoanfocus.png";
		document.images['mitem3'].src="images/Morong/dicho.png";
		$('#morelist').sfList('blur');   
		$('#morelist').sfList('hide');
		$('#morelistchedoan').sfList('show');
		$('#morelistchedoan').sfList('focus');
		$('#morelistdicho').sfList('hide');
		$('#morelistdicho').sfList('blur');
		
		document.getElementById("moretieude").innerHTML = datachedotieude[$('#morelistchedoan').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datachedonoidung[$('#morelistchedoan').sfList('getIndex')];
		
	}else if (umdex==1) {alert("umdex hien tai la: "+umdex);
		umdex++;alert("umdex hien tai laaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: "+umdex);
		document.images['mitem1'].src="images/Morong/meonauan.png";
		document.images['mitem2'].src="images/Morong/chedoan.png";
		document.images['mitem3'].src="images/Morong/dichofocus.png";
		$('#morelist').sfList('hide');     
		$('#morelist').sfList('blur');
		$('#morelistchedoan').sfList('blur');
		$('#morelistchedoan').sfList('hide');
		$('#morelistdicho').sfList('show');
		$('#morelistdicho').sfList('focus');
		
		document.getElementById("moretieude").innerHTML = datadichotieude[$('#morelistdicho').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datadichonoidung[$('#morelistdicho').sfList('getIndex')];
		
	}else if (umdex==2) {alert("umdex hien tai la: "+umdex);
		umdex=0;
		document.images['mitem1'].src="images/Morong/meonauanfocus.png";
		document.images['mitem2'].src="images/Morong/chedoan.png";
		document.images['mitem3'].src="images/Morong/dicho.png";
		$('#morelist').sfList('show');      
		$('#morelist').sfList('focus');
		$('#morelistchedoan').sfList('hide');
		$('#morelistchedoan').sfList('blur');
		$('#morelistdicho').sfList('hide');
		$('#morelistdicho').sfList('blur');
		
		//set lại noi dung
		document.getElementById("moretieude").innerHTML = datameotieude[$('#morelist').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datameonoidung[$('#morelist').sfList('getIndex')];
	}}
function MoreUp(){
	
	switch (umdex) {
	case 0:
		$('#morelist').sfList('prev');
		//setdatalist mo rong
		document.getElementById("moretieude").innerHTML = datameotieude[$('#morelist').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datameonoidung[$('#morelist').sfList('getIndex')];
		break;
	case 1:
		$('#morelistchedoan').sfList('prev');
		
		document.getElementById("moretieude").innerHTML = datachedotieude[$('#morelistchedoan').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datachedonoidung[$('#morelistchedoan').sfList('getIndex')];
		break;
	case 2:
		$('#morelistdicho').sfList('prev');
		document.getElementById("moretieude").innerHTML = datadichotieude[$('#morelistdicho').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datadichonoidung[$('#morelistdicho').sfList('getIndex')];
		break;
	}
}

function MoreDown(){
	
	switch (umdex) {
	case 0:
		$('#morelist').sfList('next');
		
		//set data morong
		alert("index cua líiiiiiiiiiiiiiippppppppppppppppppppppppppppppppppppppppiiiiiiit là: "+ $('#morelist').sfList('getIndex'));
		document.getElementById("moretieude").innerHTML = datameotieude[$('#morelist').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datameonoidung[$('#morelist').sfList('getIndex')];
		break;
	case 1:
		$('#morelistchedoan').sfList('next');
		
		document.getElementById("moretieude").innerHTML = datachedotieude[$('#morelistchedoan').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datachedonoidung[$('#morelistchedoan').sfList('getIndex')];
		break;
	case 2:
		$('#morelistdicho').sfList('next');
		
		document.getElementById("moretieude").innerHTML = datadichotieude[$('#morelistdicho').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datadichonoidung[$('#morelistdicho').sfList('getIndex')];
		break;
	}
}

function handmoreclick(){
	document.getElementById("mitem1").addEventListener('click', function() {
		umdex=0;
		document.images['mitem1'].src="images/Morong/meonauanfocus.png";
		document.images['mitem2'].src="images/Morong/chedoan.png";
		document.images['mitem3'].src="images/Morong/dicho.png";
		$('#morelist').sfList('show');      
		$('#morelist').sfList('focus');
		$('#morelistchedoan').sfList('hide');
		$('#morelistchedoan').sfList('blur');
		$('#morelistdicho').sfList('hide');
		$('#morelistdicho').sfList('blur');
		
		//set lại noi dung
		document.getElementById("moretieude").innerHTML = datameotieude[$('#morelist').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datameonoidung[$('#morelist').sfList('getIndex')];
	});
	document.getElementById("mitem2").addEventListener('click', function() {
		umdex=1;
		document.images['mitem1'].src="images/Morong/meonauan.png";
		document.images['mitem2'].src="images/Morong/chedoanfocus.png";
		document.images['mitem3'].src="images/Morong/dicho.png";
		$('#morelist').sfList('blur');   
		$('#morelist').sfList('hide');
		$('#morelistchedoan').sfList('show');
		$('#morelistchedoan').sfList('focus');
		$('#morelistdicho').sfList('hide');
		$('#morelistdicho').sfList('blur');
		
		document.getElementById("moretieude").innerHTML = datachedotieude[$('#morelistchedoan').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datachedonoidung[$('#morelistchedoan').sfList('getIndex')];
	});
	document.getElementById("mitem3").addEventListener('click', function() {
		umdex=2;alert("umdex hien tai laaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: "+umdex);
		document.images['mitem1'].src="images/Morong/meonauan.png";
		document.images['mitem2'].src="images/Morong/chedoan.png";
		document.images['mitem3'].src="images/Morong/dichofocus.png";
		$('#morelist').sfList('hide');     
		$('#morelist').sfList('blur');
		$('#morelistchedoan').sfList('blur');
		$('#morelistchedoan').sfList('hide');
		$('#morelistdicho').sfList('show');
		$('#morelistdicho').sfList('focus');
		
		document.getElementById("moretieude").innerHTML = datadichotieude[$('#morelistdicho').sfList('getIndex')];
		document.getElementById("morenoidung").innerHTML = datadichonoidung[$('#morelistdicho').sfList('getIndex')];
	});
}
//////
function SceneMore() {

	this.initialize = function () {
		alert("SceneMore.initialize()");
		$('#PopupThoatMore').sfPopup({
			text:'Bạn có muốn thoát ứng dụng không?',
			buttons:['Đồng ý', 'Hủy'],
			defaultFocus: 1, 
			callback:function(selectedIndex){
				alert('selected button index: ' + selectedIndex);
				if(selectedIndex==0){
					sf.core.exit();
				}
			}
		});
		handmoreclick();
		//load data
		LoadDataMore();
		
		$('#morelist').sfList({
			data:['Cách khử cặn đóng trong phích nước', 'Khử mùi tanh hôi trong bếp', 'Chữa mặn trong thức ăn nấu chín?', 'Muối cà trắng và giòn', 'Làm sao cho cá kho được chắc thịt ', 'Tẩy mùi tanh của chảo bằng sắt', 'Giữ táo còn trắng sau khi gọt', 'Muốn pha một ấm trà thật ngon', 'Nấu canh cải cho ngon?','Chọn Cá','Tác dụng của hành tây','Luộc gà được đẹp và ngon','Chiên cá không dính chảo','Để cắt hành mà không bị chảy nước mắt?','Làm sao cho tủ lạnh không bốc mùi khó chịu?','Lưu ý khi nấu món đậu','Bớt độ béo của món canh'],
			index:0,
			itemsPerPage: 7,
		});
		$('#morelistchedoan').sfList({
			data:['Giảm cân nhanh', 'Người bệnh suy tim', 'Chế độ ăn giúp chống nhăn da','Các loại thức ăn bổ não'],
			index:0,
			itemsPerPage: 7,
		});
		$('#morelistdicho').sfList({
			data:['Lên kế hoạch','Chọn Thịt tươi' ,'Chọn Cá tươi ','Chọn trứng có chất lượng tốt','Chọn rau quả tươi','Chọn thực phẩm đóng hộp'],
			index:0,
			itemsPerPage: 7,
		});
	};

	this.handleShow = function (data) {
		alert("SceneMore.handleShow()");
		$("#keyhelp").sfKeyHelp({
	        "MOVE" : "Di chuyển",
	        "RETURN" : "Trở về"
	    });
		
		var helpBarInfo = {
	            helpbarType: "HELPBAR_TYPE_GESTURE_CUSTOMIZE",
	            itemList: [
	                { itemType: "HELPBAR_GESTURE_ITEM_RETURN", itemText: "Go back" },
	                { itemType: "HELPBAR_GESTURE_ITEM_POINTING_GESTURE", itemText: "Choose picture" }
	            ]
	        };

	        webapis.recognition.SetGestureHelpbarInfo(JSON.stringify(helpBarInfo));
	        
	        var helpBarInfo1 = {
	                helpbarType: "HELPBAR_TYPE_VOICE_CUSTOMIZE",
	                helpbarItemsList: [
	                    { itemText: "Up", commandList: [{command: "Up"}] },
	                    { itemText: "Down", commandList: [{command: "Down"}] },
	                    { itemText: "Left", commandList: [{command: "Left"}]},
	                    { itemText: "Right", commandList: [{command: "Right"}]},
	                    { itemText: "Close", commandList: [{command: "Close"}]},
	                    { itemText: "Exit", commandList: [{command: "Exit"}]}
	                ]
	            };

	            webapis.recognition.SetVoiceHelpbarInfo(JSON.stringify(helpBarInfo1));
	};

	this.handleHide = function () {
		alert("SceneMore.handleHide()");
		// this function will be called when the scene manager hide this scene
	};

	this.handleFocus = function () {
		alert("SceneMore.handleFocus()");
		// this function will be called when the scene manager focus this scene
		Moreini();
		document.getElementById("keyhelp").style.visibility = "visible";
		GestureDispatcher.currentScene = this;
        VoiceDispatcher.currentScene = this;
        Grid.hienthiketnoi();
	};

	this.handleBlur = function () {
		alert("SceneMore.handleBlur()");
		// this function will be called when the scene manager move focus to another scene from this scene
	};
	this.handleKeyDown = function (keyCode) {
		alert("SceneMore.handleKeyDown(" + keyCode + ")");
		// TODO : write an key event handler when this scene get focued
		switch (keyCode) {
			case sf.key.LEFT:
				 MoreLeft();
				break;
			case sf.key.RIGHT:
				MoreRight();
				break;
			case sf.key.UP:
				MoreUp();
				break;
			case sf.key.DOWN:
				MoreDown();
				
			case sf.key.ENTER:
				

				break;
			case sf.key.RETURN:
				sf.scene.hide('More');
	        	sf.scene.show('Main');
				sf.scene.focus('Main');

				break;
			case sf.key.EXIT:
	        	$('#PopupThoatMore').sfPopup('show');
	        	
	            break;
			default:
				alert("handle default key event, key code(" + keyCode + ")");
				break;
		}
	};
	this.handleGesture = function(e) {
        log("SceneCookingGuide.handleGesture, type: " + e.eventtype);

        // as for now no gesture events are handled here
        // please look at ScenePhotoView.handleGesture() method
    };
    this.handleVoice = function(e) {
        log("SceneCookingGuide.handleVoicee, type: " + e.eventtype);

        switch (e.result.toLowerCase()) {
            case "up": 
            	MoreUp();
            	
            	break;
            case "down": 
            	MoreDown();
            	 
            	break;
            case "left": 
            	MoreLeft();
            	
            	 
            	break;
            case "right": 
            	
            	MoreRight();
            	 
            	break;
            
            case "close":
                sf.scene.hide("More");
                sf.scene.show("Main");
                sf.scene.focus("Main");
                sf.key.preventDefault();
                break;
            case "exit": $('#PopupThoatMore').sfPopup('show'); break;
        }
    };
};
//////////////////////